require(["config"],function(){
	require(["jquery","template","zoom","cookie","header","fly"],function($,template){
		function Detail(){
			this.render();
			this.addListener();
			this.val = 1;
			this.id;
		}
		Detail.prototype = {
			constructor:Detail,
			render:function(){
				$.ajax("http://rap2api.taobao.org/app/mock/86936/api/detail")
				.done($.proxy(this.dataHandle,this));
			},
			dataHandle:function(data){
				data = data.res_body.data;
				this.id = data.id;
				var html = template("detail_template",data);
				$(".container_box").prepend(html);
				this.zoom();
			},
			zoom:function(){
				$(".middle-zoom").elevateZoom({
					gallery:'gal1', 
					cursor: 'crosshair', 
					galleryActiveClass: 'active',
					imageCrossfade: true
				});
			},
			addListener:function(){
				$(".container_box").on("click","#sub,#add",$.proxy(this.valueHandel,this))
				$(".container_box").on("focus",".amount-txt",$.proxy(this.focusHandel,this))
				$(".container_box").on("blur",".amount-txt",$.proxy(this.inputHandel,this))
				$(".container_box").on("click",".btn-cart,.btn-buy",$.proxy(this.addToCart,this))
			},
			valueHandel:function(event){
				var src = event.target;
				var par = $(src).parents(".amount_content");
				var val = par.find(".amount-txt").val();
				if($(src).is("#sub")){
					if(val<=1){
						return;
					}
					val--;
				}else{
					val++;
				}
				par.find(".amount-txt").val(val);
			},
			focusHandel:function(){
				this.val = $(".amount-txt").val();
			},
			inputHandel:function(){
				var val = this.val;
				var input = $(".amount-txt");
				var inputVal = input.val();
				var reg = /^[1-9]\d*$/;
				if(!reg.test(inputVal)){
					input.val(val);
				}else{
					input.val(inputVal);
				}
			},
			addToCart:function(event){
				var currentProduct = {
					id:this.id,
					img:$("#gal1 img:first").attr("src"),
					cn:$(".cn").text(),
					en:$(".en").text(),
					give:$(".tip2").text(),
					amount:Number($(".amount-txt").val()),
					weight:$(".weig").text(),
					price:$("#price").text()
				};
				$.cookie.json = true;
				var products = $.cookie("cart")||[];
				var has = products.some(function(prod){
					if(prod.id == currentProduct.id){
						prod.amount += Number($(".amount-txt").val());
						return true;
					}
					return false;
				});
				if(!has){
					products.push(currentProduct);
				}
				$.cookie("cart",products,{expires:10,path:"/"});
				this.fly(event);
				var len = $.cookie("cart").length;
				$("#cart-num").text(len);
			},
			fly:function(event){
				var start = {left:event.pageX-$(window).scrollLeft(),top:event.pageY-$(window).scrollTop()};
				var end = $("#cart-icon").offset();
				end.left -= $(window).scrollLeft();
				end.top -= $(window).scrollTop();
				var flyer = $("<img>").css({
					width: 20,
					height: 20
				});
				flyer.attr("src",$(".middle-zoom").attr("src"));
				flyer.fly({
					start,
					end,
					onEnd(){
						this.destroy();
					}
				});
			}
		}
		new Detail();
	});
});